package com.zrrd.homework._0325_0331.h3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author WanJl
 * @version 1.0
 * @title Mian
 * @description
 * @create 2024/3/31
 */
public class Mian {

    List<Student> students;


    /**
     * 存储学生信息的方法
     *
     * @param list
     */
    public void addStudent(ArrayList<Student> list) {
        this.students = list;
    }

    //打印全班同学每一个人的个人信息的方法(姓名,年龄,学号,性别)
    public void printStudent() {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            // System.out.println(student);他们效果是一样的，都是调用toString()方法
            System.out.println(student.toString());
        }
    }

    /**
     * 随机对学生点名,打印学生的信息(姓名,年龄,学号,性别)
     * @return 随机到的学生信息
     */
    public Student randomStudent() {
        //Random随机对象
        Random ran=new Random();
        //通过随机的方法，随机从0开始到students.size()结束，最大是students.size()-1
        int i = ran.nextInt(students.size());
        //以i作为索引值，获取集合中的某个元素
        Student student = students.get(i);
        return student;
    }

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student s1 = new Student(1, "张三", 25, "男");
        Student s2 = new Student(2, "张思", 25, "男");
        Student s3 = new Student(3, "彰武", 25, "男");
        Student s4 = new Student(4, "张柳", 25, "男");
        Student s5 = new Student(5, "张琪", 25, "男");
        Student s6 = new Student(6, "李四", 25, "男");
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);
        Mian m = new Mian();
        m.addStudent(list);
        m.printStudent();
        Student student = m.randomStudent();
        System.out.println("随机到的同学是："+student);
    }
}
